﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;
public partial class Adcustomer_TAdCustomerLinkman : System.Web.UI.Page
{
    public int pagesize = 20;
    public string pager = "";
    public string typename = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
            if (Request["customer_id"] != null)
            {
                TAdCustomer tasi1 = new TAdCustomer(Request["customer_id"].ToString());
                tasi1.Select();
                typename = tasi1.Name;
                spanlab.InnerText = Request["customer_id"].ToString();
                //txtInfoId.Text = Request["customer_id"].ToString();
                
            }
            DBddl();
        }
        if (Request["opration"] != null)
            switch (Request["opration"].ToString())
            {
                case "edit":
                    BindInput();
                    break;
                case "ddlbind":
                    DBddl();
                    break;
            }
    }

    private void BindInput()
    {

        if (Request["id"] != null)
        {
            TAdCustomerLinkman ta = new TAdCustomerLinkman(Request["id"].ToString());
            ta.Select();
            Response.Clear();
            Response.Write(ta.ManId + "," + ta.CustomerId + "," + ta.Name + "," + ta.Sex + "," + ta.Tel + "," + ta.Mobile + "," + ta.Fax + "," + ta.Email + "," + ta.DeptName + "," + ta.Job + "," + ta.Birthday + "," + ta.ImAccount + ","+ta.Notice+","+ta.Inputer+"," + ta.InputTime.ToString());
            Response.Flush();
            Response.Close();
        }

    }
    public void Bind()
    {
        DBddl();
        string sSql = "";
        int page = 1;
        if (Request["customer_id"] != null)
        {
            TAdCustomer tasi1 = new TAdCustomer(Request["customer_id"].ToString());
            tasi1.Select();
            typename = tasi1.Name;
            spanlab.InnerText = Request["customer_id"].ToString();
            string ss = spanlab.InnerText;
            if (Request["page"] != null)
            {
                page = int.Parse(Request["page"].ToString());
                sSql = "with a as ( select (select count(*) from t_ad_customer_linkman) as countnum, row_number() over(order by input_time desc) as num,man_id,customer_id,[name],aa.infoname as dept_name,b.infoname as job,sex,tel,mobile,fax,email,birthday,im_account from t_ad_customer_linkman left join dbo.t_ad_setup_info2 as aa on aa.infoid=t_ad_customer_linkman.dept_name and aa.isshow = 1 left join dbo.t_ad_setup_info2 as b on b.infoid=t_ad_customer_linkman.job and b.isshow = 1) select * from a where num between " + ((page - 1) * pagesize + 1) + " and " + ((page - 1) * pagesize + pagesize) + "and customer_id='" + ss + "'";
            }
            else
            {
                sSql = "with a as ( select (select count(*) from t_ad_customer_linkman) as countnum, row_number() over(order by input_time desc) as num,man_id,customer_id,[name],aa.infoname as dept_name,b.infoname as job,sex,tel,mobile,fax,email,birthday,im_account from t_ad_customer_linkman left join dbo.t_ad_setup_info2 as aa on aa.infoid=t_ad_customer_linkman.dept_name and aa.isshow = 1 left join dbo.t_ad_setup_info2 as b on b.infoid=t_ad_customer_linkman.job and b.isshow = 1) select * from a where num between 1 and " + pagesize + "and customer_id='" + ss + "'";
            }
            TSqlDBReader myReader = new TSqlDBReader(sSql);
            this.Repeater1.DataSource = myReader.FDataReader;
            this.Repeater1.DataBind();
            if (myReader.FDataReader.HasRows)
            {
                int countnum = TSqlDB.ExecuteScalar(sSql);
                if (countnum % pagesize > 0)
                    pager = GenPagebar(page, countnum / pagesize + 1);
                else
                    pager = GenPagebar(page, countnum / pagesize);
            }
        }

    }

    protected void btn_insert_Click(object sender, EventArgs e)
    {
        try
        {
            TAdCustomer tasi1 = new TAdCustomer(Request["customer_id"].ToString());
            tasi1.Select();
            typename = tasi1.Name;
            spanlab.InnerText = Request["customer_id"].ToString();
            string ss = spanlab.InnerText;

            string sql = "insert into dbo.t_ad_customer_linkman(man_id,customer_id,name,sex,tel,mobile,fax,email,dept_name,job,birthday,im_account) values('" + this.txt_id.Text.Trim() + "','"+ss+"','" + this.txt_name.Text.Trim() + "','" + this.ddl_sex.Text + "','" + this.txt_tel.Text.Trim() + "','" + this.txt_mobile.Text.Trim() + "','" + this.txt_fax.Text.Trim() + "','" + this.txt_email.Text.Trim() + "','" + this.ddl_deptname.SelectedValue + "','" + this.ddl_jop.SelectedValue + "','" + this.birthday.Date + "','" + this.txt_qq.Text.Trim()+ "')";
            TSqlDB tsql = new TSqlDB();
            tsql.Execute(sql);
            //this.Response.Write("<script>");
            //this.Response.Write("alert('添加成功！')");
            //this.Response.Write("</script>");
            this.txt_id.Text = "";
            this.txt_name.Text = "";
            this.txt_tel.Text = "";
            this.txt_mobile.Text="";
            this.txt_fax.Text="";
            this.txt_qq.Text = "";
            this.txt_email.Text = "";

        }
        catch (Exception ex)
        {
            this.Response.Write("<script>alert('编号以存在，请重新编写！')</script>");
        }
        Bind();

    }

    #region 分页
    public string GenPagebar(int PageNum, int PageCounts)
    {
        string result = "<div class=\"pagebox\">";


        if (PageCounts > 1)
        {
            if (PageNum == 1)//第一页
            {
                result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a href='TAdCustomer.aspx?opration=page&page=" + i.ToString() + "'\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
                result += "<span class=\"pagebox_next\"><a href='TAdCustomerLinkman.aspx?opration=page&page=2'\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a href='TAdCustomerLinkman.aspx?opration=page&page=" + PageCounts.ToString() + "'\">尾页</a></span>";
            }
            if ((PageNum > 1) && (PageNum < PageCounts))
            {
                result += "<span class=\"pagebox_num\"><a href='TAdCustomerLinkman.aspx?opration=page&page=1' \">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a href='TAdCustomerLinkman.aspx?opration=page&page=" + i.ToString() + "'\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a href='TAdCustomerLinkman.aspx?opration=page&page=" + (PageNum - 1).ToString() + "'\">上一页</a></span>";
                result += "<span class=\"pagebox_next\"><a href='TAdCustomerLinkman.aspx?opration=page&page=" + (PageNum + 1).ToString() + "'\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a href='TAdCustomerLinkman.aspx?opration=page&page=" + PageCounts.ToString() + "'\">尾页</a></span>";
            }

            if (PageNum == PageCounts)//最后一页
            {
                result += "<span class=\"pagebox_num\"><a href='TAdCustomerLinkman.aspx?opration=page&page=1'\">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a href='TAdCustomerLinkman.aspx?opration=page&page=" + i.ToString() + "'\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a href='TAdCustomerLinkman.aspx?opration=page&page=" + (PageCounts - 1).ToString() + "'\">上一页</a></span>";
                result += "<span class=\"pagebox_next_nolink\">下一页</span>";
                result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
            }
        }
        else
        {
            result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
            result += "<span class=\"pagebox_num_nonce\"><a>1</a></span>";
            result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
            result += "<span class=\"pagebox_next_nolink\">下一页</span>";
            result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
        }

        result += "<span style=\"padding-top: 4px;\">&nbsp;(每页显示" + pagesize.ToString() + "行)</span>";
        result += "</div>";

        return result;
    }
    #endregion
    protected void Button1_Click(object sender, EventArgs e)
    {
        string sql = "";
        this.lbl_id.Text = this.HiddenField1.Value;
        this.lbl_cid.Text = this.HiddenField3.Value;
        sql = "update t_ad_customer_linkman set [name] ='" + this.txt_uname.Text.Trim() + "', sex ='" + this.ddl_usex.Text+ "', tel='" + this.txt_utel.Text.Trim() + "', mobile ='" + this.txt_umobile.Text.Trim() + "', fax ='" + this.txt_ufax.Text.Trim() + "',email='" + this.txt_uemail.Text.Trim() + "',dept_name='" + this.ddl_udeptname.SelectedValue + "',job='" + this.ddl_ujop.SelectedValue + "',birthday='" + this.ubirthday.Date + "',im_account='" + this.txt_uqq.Text.Trim() + "' where man_id='" + lbl_id.Text + "'";
        TSqlDB tsql = new TSqlDB();
        tsql.Execute(sql);
        //this.Response.Write("<script type='text/javascript'>alert('修改成功！')</script>");

        Bind();
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Bind();
    }


    protected void Repeater1_ItemCommand1(object source, RepeaterCommandEventArgs e)
    {
        string id = e.CommandArgument as string;

        if (e.CommandName == "Del")
        {
            TData.DeleteByID(id, "t_ad_customer_linkman", "man_id");
        }
        Bind();
    }
    private void DBddl()
    {
        string sql = "select infoid,infoname from t_ad_setup_info2 where typeid = 'S05'";
        TSqlDBReader myReader = new TSqlDBReader(sql);

        ddl_deptname.DataValueField = "infoid";
        ddl_deptname.DataTextField = "infoname";
        ddl_deptname.DataSource = myReader.FDataReader;
        ddl_deptname.DataBind();

        TSqlDBReader myReader2 = new TSqlDBReader(sql);
        ddl_udeptname.DataValueField = "infoid";
        ddl_udeptname.DataTextField = "infoname";
        ddl_udeptname.DataSource = myReader2.FDataReader;
        ddl_udeptname.DataBind();

        string sql1 = "select infoid,infoname from t_ad_setup_info2 where typeid = 'S06'";
        TSqlDBReader myReader3 = new TSqlDBReader(sql1);

        ddl_jop.DataValueField = "infoid";
        ddl_jop.DataTextField = "infoname";
        ddl_jop.DataSource = myReader3.FDataReader;
        ddl_jop.DataBind();

        TSqlDBReader myReader4 = new TSqlDBReader(sql1);
        ddl_ujop.DataValueField = "infoid";
        ddl_ujop.DataTextField = "infoname";
        ddl_ujop.DataSource = myReader4.FDataReader;
        ddl_ujop.DataBind();
    }
}
